package com.zh.note.huawei.twohundred.宜居星球改造计划;

import java.util.*;

/**
 * https://blog.csdn.net/hihell/article/details/130876706
 */
public class Main {
    private static int rows = 0;
    private static int cols = 0;
    private static List<int[]> coordinates = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> inputList = new ArrayList<>();
        // while (scanner.hasNext()) {
        //     inputList.add(scanner.nextLine());
        // }
        // inputList.add("YES YES NO");
        // inputList.add("NO NO NO");
        // inputList.add("YES NO NO");

        inputList.add("YES NO NO YES");
        inputList.add("NO NO YES NO");
        inputList.add("NO YES NA NA");
        inputList.add("YES NO NA NO");
        rows = inputList.size();
        cols = inputList.get(0).split(" ").length;
        String[][] grid = new String[rows][cols];

        for (int i = 0; i < rows; i++) {
            String[] strs = inputList.get(i).split(" ");
            System.arraycopy(strs, 0, grid[i], 0, cols);
        }
        int day = 0;
        //1.找出NO区域的数量
        int noNums = countNoOccurences(grid);
        boolean flag = true;
        while (noNums != 0 && flag) {
            //2.找出所有yes坐标
            List<int[]> yesList = findYes(grid);
            //3.更新所有yes区域
            for (int[] ints : yesList) {
                updateGrid(ints[0], ints[1], grid);
            }
            // 判断是否发生了更新
            if (!coordinates.isEmpty()) {
                noNums -= coordinates.size();
                coordinates.clear();
                day++;
            } else {
                flag = false;
            }

        }

        System.out.println(noNums == 0 ? day : -1);
    }

    //更新上下左右是NO的区域
    private static void updateGrid(int i, int j, String[][] grid) {
        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for (int[] dir : directions) {
            int newRow = i + dir[0];
            int newCol = j + dir[1];
            if (newRow >= 0 && newCol >= 0 && newRow < rows && newCol < cols && grid[newRow][newCol].equals("NO")) {
                // 更新符合条件的坐标
                grid[newRow][newCol] = "YES";
                //把更新的哪些坐标保存下来
                coordinates.add(new int[]{newRow, newCol});
            }

        }

    }

    //统计NO区域有几个
    private static int countNoOccurences(String[][] grid) {
        int count = 0;
        for (String[] row : grid) {
            int num = (int) Arrays.stream(row).filter(s -> s.equals("NO")).count();
            count += num;
        }
        return count;
    }

    //找到yes区域的坐标
    public static List<int[]> findYes(String[][] grid) {
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j].equals("YES")) {
                    list.add(new int[]{i, j});
                }
            }
        }
        return list;
    }
}
